<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h3 class="title">新生管理系统</h3>
      </div>

      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="Username"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <el-button type="text" style="float:right;margin-bottom:10px" @click="forgetPassword()">忘记密码?</el-button>
      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录系统</el-button>

    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { checkAccount } from '@/api/login'
import moment from 'moment'
// import JSEncrypt from 'jsencrypt'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于六位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true

          // var encryptor = new JSEncrypt() // 创建加密对象实例
          // // 之前ssl生成的公钥，复制的时候要小心不要有空格
          // var pubKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAillcDLbepalIyEMN14hRqLbZkCI19WEheOBiWPikYJ6oJYfMABt/Lq3by/2r3LV5kC7RIe1lIuXUzqfM2m6qVWoIMGrSKbvBQtcl9rgZZ5c+WpvsUa7kgcogcxIyLAqZnEyUsmWKz3lWpG2/r+82GJiDhXdThkTr1OY3RAvj3CnHHq2cw71P7NAhZaxKsB3d4QzhVZ7K4+oJEhqfbm3CXSg0CfhGcghKmigNRzQRh96G/vtUdgOAORpt1/AtCE6D06vNvEBYNkNI516BRE32hAfO3ichE2hXNoHt2H8RmI5iZtp77Lt6yV38DceJw/MS+vKtinq/XsO7EJPNPkNFzQIDAQAB'
          // encryptor.setPublicKey(pubKey)// 设置公钥
          // var rsaPassWord = encryptor.encrypt(this.loginForm.password) // 对内容进行加密
          this.$store.dispatch('user/login', { username: this.loginForm.username, password: this.loginForm.password }).then(() => {
            checkAccount({ username: this.loginForm.username, password: this.loginForm.password }).then((res) => {
              const currentTime = new Date()
              const years = moment(currentTime).diff(moment(res[0].registerTime), 'years')
              if (years < 1) {
                this.$router.push({ path: this.redirect || '/' })
                console.log('登录成功')
                this.loading = false
              } else {
                this.$message.error('当前您已经不是新生，账号已被停用')
                this.loading = false
              }
            })
          }).catch(() => {
            this.$router.push({ path: this.redirect || '/' })
            console.log('登录失败')
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    forgetPassword() {
      this.$router.push({ path: '/updatePassword' })
    }
  }
}
</script>

<style lang="scss">
 .login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      //考虑mac浏览器和火狐浏览器的兼容性，将浏览器button,input,select,textarea等默认样式清除
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      height: 47px;
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>

.login-container {
  min-height: 100%;
  width: 100%;
  background: url(~@/assets/images/login.jpg);
  overflow: hidden;
  background-size: 100% 100%;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
     // 解决可选中的问题
    user-select: none;
    // 浏览器兼容性问题
    // 火狐
    -moz-user-select:none;
    // webkit浏览器
    -webkit-user-select:none;
    // IE10
    -ms-user-select:none;
    position: relative;
    color: #454545;

    .title {
      font-size: 26px;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    cursor: pointer;
    // 解决可直接选中密码的问题
    user-select: none;
    // 火狐
    -moz-user-select:none;
    // webkit浏览器
    -webkit-user-select:none;
    // IE10
    -ms-user-select:none;
  }
}
</style>
